<template>
  <div>
    <el-dialog
      v-dialogDrag
      ref="dialog"
      :close-on-click-modal="false"
      :visible.sync="openSelectDealer"
      :before-close="cancel"
      :append-to-body="true"
      title="经销商选择"
      width="60%"
      @opened="searchHandle"
    >
      <!-- 查询条件 -->
      <el-form ref="searchForm" :model="search" size="mini" label-width="90px">
        <el-row :gutter="6">
          <el-col :xs="24" :sm="8" :md="8">
            <el-form-item prop="DEALER_CODE" label="经销商代码">
              <el-input v-model="search.DEALER_CODE" placeholder="经销商代码" clearable />
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="8" :md="8">
            <el-form-item prop="DEALER_NAME" label="经销商名称">
              <el-input v-model="search.DEALER_NAME" placeholder="经销商名称" clearable />
            </el-form-item>
          </el-col>
          <div style="text-align:right;">
            <el-button type="primary" icon="el-icon-search" @click="searchHandle">查询</el-button>
            <el-button icon="el-icon-refresh" @click="resetForm('searchForm')">重置</el-button>
          </div>
        </el-row>
      </el-form>
      <div class="tableHead">
        <div class="tabletitle">经销商列表</div>
      </div>
      <el-table
        v-loading="load"
        ref="table"
        :data="list"
        :height="160"
        highlight-current-row
        stripe
        border
        @selection-change="select"
        @current-change="select">
        <el-table-column label="序号" type="index" />
        <!-- <el-table-column width="80" /> -->
        <el-table-column prop="DEALER_CODE" label="经销商代码" min-width="100"/>
        <el-table-column prop="DEALER_NAME" label="经销商名称" min-width="200"/>
      </el-table>
      <br>
      <!-- 分页 -->
      <div style="text-align: center;color: #606266">
        <Pagination
          :data="page"
          @change-size="handleSizeChange"
          @change-page="handleCurrentChange"
        />
      </div>
      <div slot="footer" class="dialog-footer" style="text-align:center;">
        <el-button type="text">
          <i class="el-icon-search magnifier" />
        </el-button>
        <el-button type="primary" @click="confirmHandle">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>

    </el-dialog>
  </div>
</template>

<script>
import { getDealer } from '@/api/admin/auth/gms/userDrive.js'
import Pagination from '@/components/Pagination'
import formMixins from '@/mixins/formMixins'

export default {
  name: 'SelectDealer2',
  components: {
    Pagination
  },
  mixins: [formMixins],
  props: {
    allowRadio: {
      type: Boolean,
      default: false
    },
    openSelectDealer: {
      type: Boolean,
      default: false
    },
    flag: {
      type: String,
      default: '0'
    }
  },
  data() {
    return {
      // multipleSelection: [],
      list: [],
      load: false,
      page: {},
      selection: '',
      search: {
        limit: 10,
        pageNum: 1,
        DEALER_CODE: '',
        DEALER_NAME: '',
        flag: '0'
      }
    }
  },
  mounted() {
  },
  methods: {
    searchHandle() {
      this.load = true
      this.search.flag = this.flag
      getDealer(this.search)
        .then(res => {
          this.list = res.rows
          this.page = res
          this.load = false
        })
        .catch(err => {
          this.load = false
        })
    },
    handleSizeChange(size) {
      this.search.limit = size
      this.searchHandle()
    },
    handleCurrentChange(page) {
      this.search.pageNum = page
      this.searchHandle()
    },
    // handleSelectionChange(val) {
    //   if (val == null) {
    //     this.multipleSelection = []
    //   } else {
    //     this.multipleSelection = val
    //   }
    //   console.log(val)
    // },

    select(val) {
      if (val == null) {
        this.selection = []
      } else {
        this.selection = val
      }
    },

    cancel() {
      this.resetForm('searchForm')
      this.$emit('dealerClose', '')
    },

    confirmHandle() {
      if (this.selection === '') {
        this.$message({
          type: 'warning',
          message: '请选择数据!'
        })
      } else {
        this.$emit('getDealerInfo', this.selection)
        this.resetForm('searchForm')
        this.$emit('dealerClose', '')
      }
    }

  }
}
</script>

<style scoped>
</style>
